package failover

import (
	"bp/conf"
	"bp/db"
	sqlc "bp/db/sqlc"
	"context"

	"go.uber.org/zap"
)

type NsqFailover struct {
	dao *db.PgDao
	slog  *zap.SugaredLogger
	vars  *conf.VarsSection
}

// New initialization
func New(
	slog *zap.SugaredLogger,
	pgdao *db.PgDao,
	vars *conf.VarsSection,
) *NsqFailover {
	return &NsqFailover{
		slog:  slog.With("module", "NsqFailover"),
		dao: pgdao,
		vars:  vars,
	}
}

func (s *NsqFailover) HandleNsqSubFail(nsqPubID int64, errPtr *error) {
	if errPtr != nil && (*errPtr) != nil {
		ctx := context.Background()
		q := s.dao.Q
		err2 := q.UpdateNsqSubError(ctx, sqlc.UpdateNsqSubErrorParams{
			ID:     nsqPubID,
			ErrMsg: sqlc.NSFromString((*errPtr).Error()),
		})
		if err2 != nil {
			s.slog.Errorf("HandleNsqSubFail err: %s", err2.Error)
		}
	}
}
